<?php
	include "public/commom/config.php";//引入配置连接数据库文件
	include "public/commom/commom.php";//引入配置图片文件
	include "public/commom/timename.php";//引入配置时间文件
	
	$user_id = isset($_SESSION["user_id"]) ? $_SESSION["user_id"] : "";//用户id
	$username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";//昵称
	$headportrait = isset($_SESSION["headportrait"]) ? $_SESSION["headportrait"] : "";//头像
	
	
	if($_GET) {
		$weibo_id = isset($_GET["weibo_id"]) ? $_GET["weibo_id"] : "";//微博id
		$updateSelect = isset($_GET["weibo_id"]) ? "id = '$weibo_id'" : "user_id = '$user_id'";
		$updateStatus = isset($_GET["weibo_id"]) ? "weibo_id = '$weibo_id' AND status = '0'" : "weibo_id = " . $row["id"] . " AND status = '0'";
		$weiboSelect = isset($_GET["weibo_id"]) ? "weibo.id = '$weibo_id'" : "weibo.user_id = '$user_id' AND comment.status = '0'";
		
		$sql = "SELECT * FROM weibo WHERE " . $updateSelect . "";//sql语句 - 查找微博
		
		$result = mysqli_query($conn, $sql);
		
		while($row = mysqli_fetch_array($result)) {
			if($row["user_id"] == $user_id) {
				$sql1 = "UPDATE comment SET status = '1' WHERE " . $updateStatus . "";
			
				$result1 = mysqli_query($conn, $sql1);
			}
		}
	}
?>

<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
	<meta charset="UTF-8"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<meta name="renderer" content="webkit">
	<title>简易微博 评论</title>
	<!--[if lt IE 9]>
        <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
	<script type="text/javascript" src="js/jquery-1.11.3.min.js" ></script>
	<script type="text/javascript" src="js/bootstrap.min.js"></script>
	<script src="js/index.js" type="text/javascript" charset="utf-8"></script>
	<link rel="stylesheet" href="css/bootstrap.min.css" />
	<link rel="stylesheet" href="css/set.css" />
	<script type="text/javascript">
		setInterval("message(<?php echo $user_id; ?>)", 2000);
	</script>
</head>
<body>

<nav class="navbar navbar-default navbar-fixed-top ks_nav" role="navigation">
	<div class="container">
		<div class="navbar-header">
			<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#ks_nav">
				<span class="icon-bar"></span>
	        	<span class="icon-bar"></span>
	        	<span class="icon-bar"></span>
      		</button>
			<a class="navbar-brand" href="index.php">简易微博 - Beta v1.0.0</a>
		</div>

		<div class="collapse navbar-collapse" id="ks_nav">
			<form class="navbar-form navbar-left" role="search">
				<div class="form-group">
					<input type="text" class="form-control" placeholder="找人...">
				</div>
				<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-zoom-in"></span></button>
			</form>
			<ul class="nav navbar-nav navbar-right">
				<li><a href="userset.html">注册</a></li>
				<?php
					if($user_id) {
						echo "<li class='nav_user'><a href='userall.php?user_id=" . $user_id . "'><span class='glyphicon glyphicon-user'></span>" . $username . "</a></li>
							<li><a href='cancellation.php'>登出</a></li>";
					}else {
						echo "<li><a href='#' data-toggle='modal' data-target='#useron'>登陆</a></li>";
					}
				?>
				<li><a href="#">关于作者</a></li>
				<li><a href="#">返回顶部</a></li>
			</ul>
		</div>
	</div>
</nav>

<div class="container">
	<div class="row">
		
		<div class="col-md-3 col-md-offset-1 col-md-push-7">
			<?php
				if($user_id) {
					?>
						
						<!-- 登陆后 -->
						<div class="cont top_right">
							<div class="row">
								<div class="col-md-6 col-md-offset-3">
									<a href="userall.php"><img src="<?php echo $imgPath . $headportrait; ?>" width="100%" class="img-circle hidden-xs"></a>
								</div>
								<div class="col-md-12">
									<h5 class="text-center"><a href="userall.php"><?php echo $username; ?></a></h5>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12">
									<?php
										$sql = "SELECT privateletter.*,user.username FROM privateletter LEFT JOIN user ON (privateletter.user_id = user.id) WHERE privateletter.user_id = '$user_id' AND privateletter.status = '0'";//sql语句 - 查找该用户所有私信数量
										
										$result = mysqli_query($conn, $sql);//result - 执行sql语句
										
										$prinum = mysqli_num_rows($result);//每个用户私信数量
									?>
									<a class="btn btn-info btn-block" href="#">私信 <span class="badge"><?php if($prinum != 0) { echo $prinum; } ?></span></a>
								</div>
								<div class="col-md-12">
									<?php
										$sql = "SELECT comment.* FROM comment LEFT JOIN user ON (comment.user_id = user.id) LEFT JOIN weibo ON (comment.weibo_id = weibo.id) WHERE comment.user_id = '$user_id'";//sql语句 - 查找该用户所有评论数量
										
										$result = mysqli_query($conn, $sql);//result - 执行sql语句
										
										$comnum = mysqli_num_rows($result);//每个用户评论数量
									?>
									<a class="btn btn-info btn-block" href="#">评论 <span class="badge"><?php if($comnum != 0) { echo $comnum; } ?></span></a>
								</div>
							</div>
						</div>
						
					<?php
				}else {
					?>
						
						<!-- 登陆前 -->
						<div class="cont top_right user_on">
							<h4 class="text-center">登陆 / 注册</h4>
							<div class="row">
								<div class="col-md-12">
									<a class="btn btn-info btn-block" href="#" data-toggle="modal" data-target="#useron">登&nbsp;&nbsp;陆</a>
								</div>
								<div class="col-md-12">
									<a class="btn btn-info btn-block" href="userset.html">注&nbsp;&nbsp;册</a>
								</div>
							</div>
						</div>
						
					<?php
				}
			?>

			<!-- /.cont_right -->
		</div>
		
		<div class="col-md-7 col-md-pull-3">
			<?php
				$wsql = "SELECT weibo.*, user.headportrait, user.username FROM weibo LEFT JOIN user ON (weibo.user_id = user.id) LEFT JOIN comment ON (weibo.id = comment.weibo_id) WHERE " . $weiboSelect . " GROUP BY comment.weibo_id ORDER BY weibo.time DESC";//sql语句 - 查找微博
				
				$wresult = mysqli_query($conn, $wsql);//result - 执行sql语句
				
				while($row = mysqli_fetch_array($wresult)) {//循环
					$tname = timename($row["time"]);//时间
					?>
						
						<div class="cont cont_left cont_weibo">
							<div class="c_header">
								<img src="<?php echo $imgPathThumb . $row["headportrait"]; ?>" class="img-circle hidden-xs c_pho">
								<h4><?php echo $row["username"]; ?><small>发布于：<?php echo $tname; ?></small></h4>
							</div>
							<div class="c_cont">
								<p><?php echo $row["content"]; ?></p>
							</div>
							<div class="c_footer">
								<div class="btn-group btn-group-justified">
									<div class="btn-group">
										<?php
											$psql1 = "SELECT * FROM praise WHERE user_id = '$user_id' AND weibo_id = " . $row["id"] . "";//sql语句 - 查找该用户是否赞过该微博
											
											$presult1 = mysqli_query($conn, $psql1);//result - 执行sql语句
											
											$psql2 = "SELECT * FROM praise WHERE weibo_id = " . $row["id"] . "";//sql语句 - 查找微博有多少赞
											
											$presult2 = mysqli_query($conn, $psql2);//result - 执行sql语句
											
											$praisenum = mysqli_num_rows($presult2);//每条微博赞数量
											
											if($praisenum == 0) {
												$praisenum = "赞";
											}
											
											if(mysqli_fetch_array($presult1)) {//该用户是否赞过该微博
												//赞过
												echo "<a href='praise.php?weibo_id=" . $row["id"] . "' class='btn btn-info'><span style='color: orange;' class='glyphicon glyphicon-thumbs-up'></span><span style='color: orange;' class='praisenum'>" . $praisenum . "</span></a>";
											}else {
												//没赞
												echo "<a href='praise.php?weibo_id=" . $row["id"] . "' class='btn btn-info'><span class='glyphicon glyphicon-thumbs-up'></span><span class='praisenum'>" . $praisenum . "</span></a>";
											}
										?>
										
									</div>
									<div class="btn-group">
										<?php
											$csql = "SELECT comment.* FROM comment LEFT JOIN weibo ON (comment.weibo_id = weibo.id) WHERE comment.weibo_id = " . $row["id"] . " AND comment.privacy = '1' OR comment.weibo_id = " . $row["id"] . " AND comment.user_id = '$user_id'";//sql语句 - 查找该微博所有评论
											
											$cresult = mysqli_query($conn, $csql);//result - 执行sql语句
											
											$commentnum = mysqli_num_rows($cresult);//微博评论总数
										?>
										<a href="usercom.php?weibo_id=<?php echo $row["id"]; ?>" class="btn btn-info">评论 <span class="badge"><?php if($commentnum != 0) { echo $commentnum; } ?></span></a>
									</div>
									<div class="btn-group">
										<a href="#" class="btn btn-info">私信</a>
									</div>
								</div>
							</div>
							
							<!--评论-->
							<div class="con_comment">
								<div class="c_title">
									<h5>评论</h5>
								</div>
								<?php
									$csql = "SELECT comment.*, user.username , user.headportrait FROM comment LEFT JOIN user ON (comment.user_id = user.id) LEFT JOIN weibo ON (comment.weibo_id = weibo.id) WHERE comment.weibo_id = '$weibo_id' AND (comment.privacy = '1' OR weibo.user_id = '$user_id' OR (comment.privacy = '0' AND user.id = '$user_id')) ORDER BY time DESC";//sql语句 - 查找该微博所有评论								
									
									$cresult = mysqli_query($conn, $csql);//result - 执行sql语句
									
									while($row = mysqli_fetch_array($cresult)) {//循环
										$tname = timename($row["time"]);
										?>
											
											<div class="c_comment">
												<div class="com_header">
													<img src="<?php echo $imgPathThumb . $row["headportrait"]; ?>" class="hidden-xs c_pho" width="30px">
													<h4><?php echo $row["username"]; ?>：<span><?php echo $row["content"]; ?></span></h4>
													<p><?php echo $tname; ?></p>
													<?php
														if($row["privacy"] == 0) {
															echo "<span class='glyphicon glyphicon-user c_pri'></span>";
														}
													?>
												</div>
												<div class="c_btn">
													<a href='javascript:;'>点赞</a>&nbsp;|
													<a href='javascript:;'>回复</a>
												</div>
											</div>
										
										<?php	
									}
								?>
							</div>
							
						</div>
						
					<?php
				}
			?>
			<!-- /.cont_left -->
			
			<div class="cont cont_comment">
				<h4>分享每天好心情...</h4>
				<form action="comment.php" method="post" enctype="multipart/form-data">
					<textarea name="content" class="form-control" rows="7" placeholder="说明文字..."></textarea>
					
					<div class="row">
						<div class="col-md-3 col-md-offset-6">
							<select class="form-control" name="privacy">
								<option value="1" selected>所有人可见</option>
								<option value="0">仅用户可见</option>
							</select>
						</div>
						<div class="col-md-3">
							<input type="hidden" name="weibo_id" value="<?php echo $weibo_id; ?>" />
							<input type="submit" class="btn btn-primary btn-block" value="发布">
						</div>
					</div>
					
				</form>
			</div>
			<!-- /.top_left -->
		</div>

	</div>
</div>

<!-- 用户登陆弹框 -->
<div class="modal fade" id="useron" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
				<h4 class="modal-title" id="myModalLabel">用户登陆</h4>
			</div>
			<form action="login.php" method="post" role="form">
				<div class="modal-body">
					<div class="form-group">
						<label>账号：</label>
						<input type="text" name="account" class="form-control" placeholder="请输入您的账号..." autocomplete="off">
					</div>
					<div class="form-group">
						<label>密码：</label>
						<input type="password" name="password" class="form-control" placeholder="请输入您的密码..." autocomplete="off">
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<input type="submit" class="btn btn-primary" value="登陆">
				</div>
			</form>
		</div>
	</div>
</div>

</body>
</html>
